using AutoMapper;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Domain.Entities;
using UwinEducation.Domain.IRepositories;
using UwinEducation.Shared.Models;

namespace UwinEducation.Application.Services
{
    public class CourseService : ICourseService
    {
        private readonly ICourseRepository _courseRepository;
        private readonly ITeacherScheduleRepository _teacherScheduleRepository;
        private readonly IMapper _mapper;

        public CourseService(
            ICourseRepository courseRepository,
            ITeacherScheduleRepository teacherScheduleRepository,
            IMapper mapper
        )
        {
            _courseRepository = courseRepository;
            _teacherScheduleRepository = teacherScheduleRepository;
            _mapper = mapper;
        }

        public async Task<PagedResult<CourseDto>> GetPagedListAsync(CoursePageRequest pageRequest)
        {
            var result = await _courseRepository.GetPagedListAsync(pageRequest);
            return new PagedResult<CourseDto>
            {
                Total = result.Total,
                Items = _mapper.Map<List<CourseDto>>(result.Items)
            };
        }

        public async Task<CourseDto> GetByIdAsync(Guid id)
        {
            var course = await _courseRepository.GetDetailsByIdAsync(id);
            if (course == null)
            {
                throw new ArgumentException($"课程不存在: {id}");
            }

            return _mapper.Map<CourseDto>(course);
        }

        public async Task<CourseDto> CreateAsync(CreateCourseDto input)
        {
            if (await _courseRepository.ExistsByCodeAsync(input.Code))
            {
                throw new ArgumentException($"课程编号已存在: {input.Code}");
            }

            var course = Course.Create(
                input.Code,
                input.Name,
                input.CalendarColor??string.Empty,
                input.Remarks ?? string.Empty
            );

            await _courseRepository.AddAsync(course);
            return await GetByIdAsync(course.Id);
        }

        public async Task<CourseDto> UpdateAsync(UpdateCourseDto input)
        {
            var course = await _courseRepository.GetByIdAsync(input.Id);
            if (course == null)
            {
                throw new ArgumentException($"课程不存在: {input.Id}");
            }

            if (await _courseRepository.ExistsByCodeAsync(input.Code, input.Id))
            {
                throw new ArgumentException($"课程编号已存在: {input.Code}");
            }

            course.Code = input.Code;
            course.Name = input.Name;
            course.CalendarColor = input.CalendarColor ?? course.CalendarColor;
            course.Remarks = input.Remarks ?? course.Remarks;

            await _courseRepository.UpdateAsync(course);
            return await GetByIdAsync(course.Id);
        }

        public async Task DeleteAsync(Guid id)
        {
            // 检查TeacherSchedule是否存在
            var teacherSchedules = await _teacherScheduleRepository.GetByCourseIdAsync(id);
            if (teacherSchedules.Any())
            {
                throw new ArgumentException($"课程: {teacherSchedules.First().Course.Name} 存在老师课程，不能删除");
            }

            var course = await _courseRepository.GetByIdAsync(id);
            if (course == null)
            {
                throw new ArgumentException($"课程不存在: {id}");
            }

            await _courseRepository.DeleteAsync(id);
        }
    }
}